// MyView.cpp : implementation file
//

#include "stdafx.h"
#include "hanoi.h"
#include "MyView.h"
#include "HanoiDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern 	CPillar g_Pillar[3];

CMyView::CMyView()
{
	m_LButtonNum=0;
}

CMyView::~CMyView()
{
}


BEGIN_MESSAGE_MAP(CMyView, CWnd)
	//{{AFX_MSG_MAP(CMyView)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers

void CMyView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	CRect rect;
	GetClientRect(rect);
	g_Pillar[0].Display(dc,'A');
	g_Pillar[1].Display(dc,'B');
    g_Pillar[2].Display(dc,'C');
}



int CMyView::CreateView(CRect rect, CWnd *pParent)
{	
	m_pParent=pParent;
	HCURSOR hcur=AfxGetApp()->LoadCursor(IDC_CURSOR);
	HBRUSH hbr=::CreateSolidBrush(RGB(250,230,255));
   CString wndClass=AfxRegisterWndClass(NULL,hcur,hbr,NULL);
   Create(wndClass,"zhu",WS_CHILD|WS_VISIBLE ,rect,pParent,ID_MY,NULL);
	return 1;
}
extern void hanoi(int n,char from ,char by,char to);
UINT ThreadFuc( LPVOID pParam )
{
	hanoi(10,'a','b','c');	
	return 1;
}
void CMyView::OnRun(int numPlate,int option) 
{

	// TODO: Add your command handler code here
	CListBox *pLB=(CListBox*)(CHanoiDlg*)AfxGetMainWnd()->GetDlgItem(IDC_LISTME);
	pLB->ResetContent();

	m_LButtonNum=0;

	((CHanoiApp*)AfxGetApp())->InitPillar(10);
	((CHanoiDlg*)m_pParent)->m_Progress.SetPos(1);
	
	m_pParent->Invalidate();
	//hanoi(numPlate,'a','b','c');
	AfxBeginThread(ThreadFuc,&numPlate);
	if(option==0)
	  SetTimer(1,50,NULL);

}



void CMyView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	
	CListBox *pLB=(CListBox*)(CHanoiDlg*)AfxGetMainWnd()->GetDlgItem(IDC_LISTME);
	if(m_LButtonNum>=pLB->GetCount())
	{
		KillTimer(1);
		Invalidate();
		return;
	}
	int plate;
	CString str;
	pLB->GetText(m_LButtonNum++,str);
	if(pLB->SetSel(m_LButtonNum-1)==LB_ERR)
		return;
	g_Pillar[str[0]-97].Pop(plate,*GetWindowDC());
	g_Pillar[str[4]-97].Push(plate,*GetWindowDC());
	CWnd::OnLButtonDown(nFlags, point);
}

void CMyView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	TRACE("%in the timer");

	int numPlate=((CHanoiDlg*)AfxGetMainWnd())->m_numPlate;
	for(int j=0;j<numPlate;j++)
		if(g_Pillar[2].m_array[j]==numPlate-j)
			((CHanoiDlg*)AfxGetMainWnd())->m_Progress.SetPos(j+1);
	::SendMessage(m_hWnd,WM_LBUTTONDOWN,0,0);
	CWnd::OnTimer(nIDEvent);
}

void CMyView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CWnd::OnMouseMove(nFlags, point);
}
